/** 
 * 自定义摇一摇
 * */
 
 
 
 
 
var SHAKE_THRESHOLD = 3000;
var last_update = 0;
var x = 0, y = 0, z = 0,
	last_x = 0, last_y = 0, last_z = 0;
mui.plusReady(function () {
	//自动加载
	(function($, owner) {
		init();
	}(mui, window.app = {}));
});
// 初始化判断
function init() {
	if (window.DeviceMotionEvent) {
		window.addEventListener(
			'devicemotion', deviceMotionHandler, false
		);
	} else {
		mui.alert('不支持摇一摇事件');
	}
}
// 摇一摇动作
function deviceMotionHandler(eventData) {
　　var acceleration = eventData.accelerationIncludingGravity;
	var curTime = new Date().getTime();
	var diffTime = curTime -last_update;
	if (diffTime > 100) {
		last_update = curTime;
		x = acceleration.x;
		y = acceleration.y;
		z = acceleration.z;
		var abs1 = x + y + z;
		var abs2 = last_x + last_y + last_z;
		var abs = abs1 - abs2;
		var speed = Math.abs(abs) / diffTime * 10000;
		if (speed > SHAKE_THRESHOLD) {
			// mui.alert("shaked");
			// 摇一摇结果，自定义业务逻辑
			setGoldAdd();
		}
		last_x = x;
		last_y = y;
		last_z = z;
　　}
　　}
// 处理金币奖励
function setGoldAdd(){
	var randInt = getRandomInt(1,5);
	// 系统参数
	var sys = getSys();
	var domain = sys.domain + '/app/reward/shake/add';
	// 用户信息
	var userInfo = cacheGet(userInfoKey);
	var uid = userInfo ? userInfo.uid : 0;
	mui.ajax(domain,{
		data:{
			appAccess: sys.appAccess,
			uid: uid,
			num: randInt
		},
		dataType:'json', //服务器返回json格式数据
		type:'post', //HTTP请求类型
		timeout:10000, //超时时间设置为10秒
		headers:{
			'Content-Type':'application/x-www-form-urlencoded',
		}, 
		success:function(res){
			if (res.code==-1) {
				mui.toast('需要更新系统参数',{ 
					duration:'short', type:'div',
				});
				setTimeout(function(){
					setSys();
					jumpToRewardShake();
				},500);
			} else if (res.code!=0) {
				mui.toast(res.msg);
				return
			}
			mui.toast('摇一摇成功',{ 
				duration:'short', type:'div',
			});
			var shakeId = res.data.lastId;
			var shakeDate = res.data.createTime;
			var one = {
				shakeId:shakeId,
				shakeNum:randInt,
				shakeDate:shakeDate
			};
			// 更新缓存
			setShakeCache(one);
			// 刷新页面
			setTimeout(function(){
				jumpToRewardShakeList();
			}, 1000);
		},
		error:function(xhr,type,errorThrown){
			mui.toast('网络错误！',{ 
				duration:'short', type:'div',
			});
		}
	});
}
// 获取数据缓存
function getShakeCache(){
	var resList = cacheGet(rewardShakeList);
	if (!resList) {
		var list = new Array(), //数据数组
			idstring = ''; //ID字符串集合
		return {
			// 数据数组
			list: list,
			// 历史ID集合
			idstr: idstring
		};
	}
	return resList;
}
/** 
* 设置数据缓存
* one 新数据一个
* */
function setShakeCache(one){
	var resList = getShakeCache();
	var list = resList.list,  //数据数组
		idstring = resList.idstr; //历史ID字符串集合
	if (list.length==0) {
		idstring = one.signId;
	} else {
		idstring = idstring+','+one.signId;
	}
	list[list.length] = one;
	var shakeNew = {
		// 数据数组
		list: list,
		// 历史ID字符串集合
		idstr: idstring,
	};
	cacheSet(rewardShakeList, shakeNew);
}